package com.kaishengit.web;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.kaishengit.entity.Goal;
import com.kaishengit.entity.Task;
import com.kaishengit.service.TaskService;
import com.kaishengit.util.web.ServletUtil;

public class SaveNewTaskServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		TaskService taskservService = new TaskService();
		
		String name = request.getParameter("name");
		String starttime = request.getParameter("starttime");
		String endtime = request.getParameter("endtime");
		String description = request.getParameter("description");
		String stateValue = request.getParameter("state");
		int state = Integer.parseInt(stateValue);
		String level = request.getParameter("level");
		String id = request.getParameter("manage");
		int userId = Integer.parseInt(id);
		
		Goal goal = (Goal) ServletUtil.getSession(request).getAttribute("curr_goal");
		int goalId = goal.getId();
		
		Task task = new Task();
		task.setName(name);
		task.setStartTime(starttime);
		task.setEndTime(endtime);
		task.setDescription(description);
		task.setState(state);
		task.setLevel(level);
		task.setUserId(userId);
		task.setGoalId(goalId);
		
		taskservService.save(task);
		
		
		ServletUtil.redirect(response, "task.jspx?id=goalId");
	}

}
